<template>

  <div class="app-container">

    <el-form :model="queryForm" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">

      <el-form-item label="项目类型" prop="projectType">
        <el-select v-model="queryForm.projectType" placeholder="请选择项目类型" clearable >
          <el-option
            v-for="dict in dict.type.project_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="费目" prop="feeType">
        <el-select v-model="queryForm.feeType" placeholder="请选择费目" clearable >
          <el-option
            v-for="dict in dict.type.fee_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="服务名称" prop="projectName">
        <el-input
          v-model="queryForm.projectFeeName"
          placeholder="请输入服务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:project:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:project:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:project:remove']"
        >删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :data="projectList" border size="small" style="width: 100%" @selection-change="handleSelectionChange" fit v-loading="loading">

      <el-table-column type="selection" width="55" align="center" fixed/>

      <el-table-column label="ID" prop="projectFeeId" width="55" align="center"/>

      <el-table-column label="项目类型" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_type" :value="scope.row.projectType"/>
        </template>
      </el-table-column>

      <el-table-column label="费目" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fee_type" :value="scope.row.feeType"/>
        </template>
      </el-table-column>

      <el-table-column label="服务名称" prop="projectFeeName" align="center" />

      <el-table-column label="码头" prop="companyCod" align="center" :formatter="companyCodeFormat" />

      <el-table-column label="默认税点" prop="taxRate" align="center" />

      <el-table-column label="创建人" prop="createBy" align="center" />

      <el-table-column label="创建时间" prop="createTime" align="center" />

      <el-table-column label="更新人" prop="updateBy" align="center" />

      <el-table-column label="更新时间" prop="updateTime" align="center" />

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryForm.pageNum"
      :limit.sync="queryForm.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>

      <el-form :model="form" ref="form" :inline="true" label-width="80px" :rules="rules">

        <el-form-item label="项目类型" prop="projectType">
          <el-select v-model="form.projectType" placeholder="请选择项目类型" clearable >
            <el-option
              v-for="dict in dict.type.project_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="费目" prop="feeType">
          <el-select v-model="form.feeType" placeholder="请选择费目" clearable >
            <el-option
              v-for="dict in dict.type.fee_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="服务名称" prop="projectFeeName">
          <el-input
            v-model="form.projectFeeName"
            placeholder="请输入服务名称"
            clearable
          />
        </el-form-item>

        <el-form-item label="默认税点" prop="taxRate">
          <el-input
            v-model="form.taxRate"
            placeholder="请输入默认税点"
            clearable
            type="number"
          />
        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

    </el-dialog>

  </div>

</template>

<script>
import {

  addProjectFee,
  delProjectFee,
  getProjectFee,
  listProjectFee,

  updateProjectFee
} from "@/api/base/project";
import {getCompanyCodeOptions} from "@/api/options/options";


export default {
  name: "Project",
  dicts:['fee_type','project_type'],
  data(){
    return{

      loading:false,

      // 显示搜索条件
      showSearch: true,

      queryForm:{
        pageNum:1,
        pageSize:10
      },

      projectList:[],

      total:0,

      companyCodeOptions:[],

      ids:[],
      single:true,
      multiple:true,

      form:{
        projectFeeId:null,
        feeType:null,
        projectFeeName:null,
        taxRate:null,
        projectType:null,
      },

      open:false,
      title:'',
      rules: {
        feeType: [
          {required: true, message: "费目不能为空", trigger: "blur"}
        ],
        projectFeeName: [
          {required: true, message: "服务名称不能为空", trigger: "blur"}
        ],
        projectType: [
          {required: true, message: "项目类型不能为空", trigger: "blur"}
        ],
      },

    }
  },
  created(){
    this.getCompanyCodeLabel()
    this.handleQuery();
  },
  methods:{

    getCompanyCodeLabel() {
      getCompanyCodeOptions().then(res => {
        this.companyCodeOptions = res.data;
      });
    },

    handleQuery(){
      this.queryForm.pageNum = 1
      this.getList()
    },

    getList(){

      this.loading = true

      listProjectFee(this.queryForm).then(res=>{

        this.total = res.total

        this.projectList = res.rows

      }).finally(()=>{
        this.loading = false
      })

    },

    resetQuery() {
      this.queryForm={
        pageNum:1,
        pageSize:10
      };
      this.handleQuery();
    },

    companyCodeFormat(row,column){

      return this.selectDictLabel(this.companyCodeOptions,row.companyCod)

    },

    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.projectFeeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    cancel() {
      this.open = false;
      this.reset();
    },

    reset() {
      this.form = {
        projectFeeId:null,
        feeType:null,
        projectFeeName:null,
        taxRate:null,
        projectType:null,
      };
      this.resetForm("form");
    },

    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.projectFeeId != null) {
            updateProjectFee(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProjectFee(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加服务";
    },

    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProjectFee(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改服务";
      });
    },

    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除服务编号为"' + ids + '"的数据项？').then(function() {
        return delProjectFee(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

  }
}
</script>

<style scoped>

</style>
